//
//  MarkaModelCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/21/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit

//
///// Protocol of MarkaModelCell
//protocol MarkaModelCellDelegate {
//    func selectItem (id: Int, index: Int)
//    func selectFullCell (id: Int)
//    func didSelectMarka(id: Int)
//}
//
//
//final class MarkaModelCell: UITableViewCell {
//    // MARK: - UI Elements
//    lazy private var itemView: MarkaModelItem = {
//        let iView = MarkaModelItem(isExpandedItem: false)
//        return iView
//    }()
//    private var modelsView: UIStackView = {
//        let stackView = UIStackView()
//        stackView.axis = NSLayoutConstraint.Axis.vertical
//        stackView.alignment = .fill
//        stackView.distribution = .fillEqually
//        stackView.spacing = 8.0
//        return stackView
//    }()
//    public var delegate: MarkaModelCellDelegate!
//    // MARK: - Cell Icon Image
//    public var isMarka: Bool = true {
//        didSet {
//            self.showHideCheckBoxes()
//            var image:UIImage!
//            if (isMarka) {
//               // image = UIImage(named: data.image ?? "") ?? nil
//                itemView.updateIcon(icon: image)
//            } else {
////                if (data.items != nil && data.items!.count > 0){
////                    image = UIImage(named: "arrow")
////                    itemView.updateIcon(icon: image)
////                    itemView.iconImg.snp.updateConstraints { (make) in
////                        make.width.equalTo(8)
////                        make.height.equalTo(13)
////                    }
////                } else {
////                    itemView.updateIcon(icon: nil)
////                }
//            }
//        }
//    }
//    /// variable that holds all the data of cell
//    public var data: SerieModel! {
//        didSet {
//            self.showHideCheckBoxes()
//            itemView.id = data.id
//            //itemView.updateText(title: data.name_ru)
//            if (isMarka) {
//                itemView.iconImg.snp.updateConstraints { (make) in
//                    make.width.equalTo(16)
//                    make.height.equalTo(16)
//                }
//            } else {
//                itemView.iconImg.snp.updateConstraints { (make) in
//                    make.width.equalTo(8)
//                    make.height.equalTo(13)
//                }
//            }
//        }
//    }
//    public var isExpanded: Bool = false {
//        didSet  {
//            if (oldValue != isExpanded) {
//                if (isExpanded) {
//                    self.contentView.addSubview(modelsView)
//                    modelsView.snp.makeConstraints { (make) in
//                        make.top.equalTo(itemView.snp.bottom).offset(8)
//                        make.leading.trailing.bottom.equalToSuperview()
//                    }
//                    self.addUIModels()
//                } else {
//                    self.hideUIModels()
//                }
//            }
//        }
//    }
//
//
//
//
//    private func showHideCheckBoxes () {
//        if (isMarka) {
//            itemView.checkBox.addIcon(icon: UIImage(named: "arrow")!)
//            self.deselectCheckBox(checkBox: itemView.checkBox)
//        } else {
//            itemView.checkBox.addIcon(icon: nil)
////            if let items = data.selectedItems {
////                self.selectItems(items: data.selectedItems!)
////                if (items.count == data.items?.count) {
////                    self.selectCheckBoxStyle(checkBox: itemView.checkBox)
////                } else {
////                    self.deselectCheckBox(checkBox: itemView.checkBox)
////                }
////            } else {
////                self.deselectCheckBox(checkBox: itemView.checkBox)
////                self.selectItems(items: [])
////            }
//        }
//    }
//
//    private func selectCheckBoxStyle (checkBox: ViewWithImage) {
//        checkBox.backgroundColor = UIColor(red: 47/255, green: 130/255, blue: 210/255, alpha: 1)
//        checkBox.addIcon(icon: UIImage(named: "correct"))
//        checkBox.layer.borderColor = UIColor.clear.cgColor
//    }
//
//    private func deselectCheckBox (checkBox: UIView) {
//        checkBox.backgroundColor = .clear
//        checkBox.layer.borderColor = self.isMarka ? UIColor.clear.cgColor : UIColor(red: 211/255, green: 220/255, blue: 230/255, alpha: 1).cgColor
//    }
//    private func selectItems (items: [Int]) {
//        for view in modelsView.arrangedSubviews {
//            self.deselectCheckBox(checkBox: (view as! MarkaModelItem).checkBox)
//        }
//        for item in items {
//            let view = modelsView.arrangedSubviews.first {
//                $0.tag == item
//            }
//            if (view != nil) {
//                self.selectCheckBoxStyle(checkBox: (view as! MarkaModelItem).checkBox)
//            }
//        }
//    }
//    // MARK: - Add Models Expandable
//    private func addUIModels () {
////        for (i, item) in data.items!.enumerated() {
////            let view = MarkaModelItem(isExpandedItem: true)
////            view.tag = i
////            view.layer.borderWidth = 1.0
////            view.layer.borderColor = UIColor(red: 229/255, green: 233/255, blue: 242/255, alpha: 1).cgColor
////            view.layer.cornerRadius = 10.0
////            view.updateText(title: item)
////            view.updateIcon(icon: nil)
////            view.delegate = self
////            let tap = GestureWithTag(target: self, action: #selector(onTap(sender:)))
////            tap.tag = i + 1
////            view.id = i
////            view.addGestureRecognizer(tap)
////            modelsView.addArrangedSubview(view)
////            itemView.iconImg.transform = CGAffineTransform(rotationAngle: .pi / 2)
////        }
////        self.selectItems(items: data.selectedItems ?? [])
//    }
//    // MARK: - Remove Models Expandable/Hide
//    private func hideUIModels () {
//        _ = modelsView.subviews.map {
//            x in x.removeFromSuperview()
//        }
//        modelsView.removeFromSuperview()
//        itemView.iconImg.transform = .identity
//    }
//
//    // MARK: - UI Actions
//    @objc func onTap (sender: GestureWithTag) {
//        delegate.selectItem(id: data.id, index: sender.tag - 1)
//    }
//
//    // MARK: - Init
//    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
//        super.init(style: style, reuseIdentifier: reuseIdentifier)
//        self.contentView.addSubview(itemView)
//        itemView.addBorder(width: itemView.frame.width,
//                           color: UIColor(red: 229/255, green: 233/255, blue: 242/255, alpha: 1),
//                           height: 1, isTop: false)
//        itemView.delegate = self
//        itemView.snp.makeConstraints { (make) in
//            make.top.equalToSuperview().offset(8)
//            make.leading.trailing.equalToSuperview()
//            make.height.equalTo(64)
//        }
//    }
//    required init?(coder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
//}
//
//
//extension MarkaModelCell: MarkaModelItemDelegate {
//    func checkBoxDidSelect(id: Int) {
//        delegate.selectFullCell(id: id)
//    }
//    func didSelectMarka(id: Int) {
//        delegate.didSelectMarka(id: id)
//    }
//    func onItemCheckBoxClick(tag: Int) {
//        delegate.selectItem(id: data.id, index: tag)
//    }
//}
